package com.eralink.unionuhfreader.util;

import com.eralink.unionuhfreader.bean.EPCBean;

import java.util.Locale;

public class EPCReportDataParseUtil {
    public static void parseInventoryEPCReport(CacheNode cacheNode, final EPCBean epcBean) {
        final byte reader_id = cacheNode.getBuffer()[0];
        epcBean.setReaderId(reader_id);
        // pc(2 bytes) + epc + crc(2 bytes) + ant(1 byte) + rssi(2bytes) [+ report_time[6 bytes]
        final int epcLen = ((cacheNode.getBuffer()[1] & 0xFF) >> 3) * 2;
        epcBean.setPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 1, 2));
        epcBean.setEPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 3, epcLen));
        epcBean.setAnt(cacheNode.getBuffer()[5 + epcLen]);

        final int rssiValue = 0xFFFF - (cacheNode.getBuffer()[6 + epcLen] & 0xFF) - ((cacheNode.getBuffer()[7 + epcLen] & 0xFF) << 8);
        epcBean.setRssi(String.format(Locale.getDefault(), "-%.1f", rssiValue / 10.0));
        if (13 + epcLen ==  cacheNode.getBufferSize()) {
            final int year = 2000 + (cacheNode.getBuffer()[8 + epcLen] & 0xFF);
            final int month = cacheNode.getBuffer()[9 + epcLen] & 0xFF;
            final int date = cacheNode.getBuffer()[10 + epcLen] & 0xFF;
            final int hour = cacheNode.getBuffer()[11 + epcLen] & 0xFF;
            final int minute = cacheNode.getBuffer()[12 + epcLen] & 0xFF;
            final int second = cacheNode.getBuffer()[13 + epcLen] & 0xFF;
            epcBean.setReportTime(String.format(Locale.CHINA, "%d-%d-%d %2d:%2d:%2d", year, month, date, hour, minute, second));
        }

        epcBean.setKey(String.format("%s-%s", epcBean.getReaderId(), epcBean.getEPC()));
    }

    public static void parseMultiBankAutoReport(CacheNode cacheNode, final EPCBean epcBean) {
        final byte reader_id = cacheNode.getBuffer()[0];
        epcBean.setReaderId(reader_id);
        final int pc_epc_len = cacheNode.getBuffer()[1] & 0xFF;
        epcBean.setPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 2, 2));
        epcBean.setEPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 4, pc_epc_len - 4));
        final int bank = cacheNode.getBuffer()[2 + pc_epc_len] & 0xFF;
        final int tag_data_len = cacheNode.getBuffer()[3 + pc_epc_len] & 0xFF;
        final String tag_data= StringUtils.bytesToHexString(cacheNode.getBuffer(), 4 + pc_epc_len, tag_data_len);
        if (0 == bank) { // 0 - RESERVE, 2 - TID, 3 - USER
            epcBean.setReserve(tag_data);
        } else if (2 == bank) {
            epcBean.setTid(tag_data);
        } else if (3 == bank) {
            epcBean.setUser(tag_data);
        }
        final int ant = cacheNode.getBuffer()[4 + pc_epc_len + tag_data_len] & 0xFF;
        epcBean.setAnt(ant);

        epcBean.setKey(String.format("%s-%s", epcBean.getReaderId(), epcBean.getEPC()));
    }

    public static void parseAutoReadReport(CacheNode cacheNode, final EPCBean epcBean) {
        final byte reader_id = cacheNode.getBuffer()[0];
        epcBean.setReaderId(reader_id);
        final int bank = cacheNode.getBuffer()[1] & 0xFF;
        if (0 == bank) { // EPC - EPC_LEN+(PC+EPC+CRC)+ANT_NUM+RSSI
            final int pc_epc_len = cacheNode.getBuffer()[2] & 0xFF;
            final int epc_len = ((cacheNode.getBuffer()[3] & 0xFF) >> 3) * 2;
            epcBean.setPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 3, 2));
            epcBean.setEPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 5, epc_len));
            epcBean.setAnt(cacheNode.getBuffer()[3 + pc_epc_len] & 0xFF);
            final int rssiValue = 0xFFFF - (cacheNode.getBuffer()[4 + pc_epc_len] & 0xFF) - ((cacheNode.getBuffer()[5 + pc_epc_len] & 0xFF) << 8);
            epcBean.setRssi(String.format(Locale.getDefault(), "-%.1f", rssiValue / 10.0));
        } else if (0x01 == bank || 0x03 == bank) { // 1: TID_LEN+TID_DATA+ANT_NUM, 3: USER_LEN+USER_DATA+ANT_NUM
            final int tag_data_len = cacheNode.getBuffer()[2] & 0xFF;
            final String tag_data = StringUtils.bytesToHexString(cacheNode.getBuffer(), 3, tag_data_len);
            if (0x01 == bank) {
                epcBean.setTid(tag_data);
            } else {
                epcBean.setUser(tag_data);
            }
            epcBean.setAnt(cacheNode.getBuffer()[3 + tag_data_len] & 0xFF);
        } else /*if (0x02 == bank || 0x04 == bank)*/ { // 2: EPC_LEN+(PC+EPC+CRC)+TID_LEN+TID_DATA+ANT_NUM, 4: PC_LEN+(PC+EPC+CRC)+USER_LEN+USER_DATA+ANT_NUM
            final int pc_epc_len = cacheNode.getBuffer()[2] & 0xFF;
            final int epc_len = ((cacheNode.getBuffer()[3] & 0xFF) >> 3) * 2;
            epcBean.setPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 3, 2));
            epcBean.setEPC(StringUtils.bytesToHexString(cacheNode.getBuffer(), 5, epc_len));
            final int tag_data_len = cacheNode.getBuffer()[3 + pc_epc_len] & 0xFF;
            final String tag_data = StringUtils.bytesToHexString(cacheNode.getBuffer(), 4 + pc_epc_len, tag_data_len);
            final int ant = cacheNode.getBuffer()[4 + pc_epc_len + tag_data_len] & 0xFF;
            if (0x02 == bank) {
                epcBean.setTid(tag_data);
            } else {
                epcBean.setUser(tag_data);
            }
            epcBean.setAnt(ant);
        }

        if (epcBean.getEPC() != null) {
            epcBean.setKey(String.format("%s-%s", epcBean.getReaderId(), epcBean.getEPC()));
        } else if (epcBean.getTid() != null) {
            epcBean.setKey(String.format("%s-%s", epcBean.getReaderId(), epcBean.getTid()));
        } else if (epcBean.getUser() != null) {
            epcBean.setKey(String.format("%s-%s", epcBean.getReaderId(), epcBean.getUser()));
        }
    }
}
